package net.shopxx.action.admin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.shopxx.entity.Properties;
import net.shopxx.entity.PropertiesValue;
import net.shopxx.service.PropertiesService;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - Properties
 */

@ParentPackage("admin")
public class PropertiesAction extends BaseAdminAction {

	private static final long serialVersionUID = -1066168819528324882L;

	private Properties properties;

	private String[] ids;
	private String[] lables;
	private String[] values;

	List<PropertiesValue> list = new ArrayList<PropertiesValue>();

	@Resource
	private PropertiesService propertiesService;

	// 列表
	public String list() {
		pager = propertiesService.findByPager(pager);
		return LIST;
	}

	// 删除
	public String delete() throws Exception {
		propertiesService.delete(ids);
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		properties = propertiesService.load(id);
		return INPUT;
	}

	// 保存
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "properties.enumLable", message = "枚举名称不允许为空!")})
	@InputConfig(resultName = "error")
	public String save() throws Exception {
		
		if(lables != null && lables.length > 0)
		{
			List<PropertiesValue> list = new ArrayList<PropertiesValue>();
			for (int i=0; i < lables.length; i++) {
				PropertiesValue value = new PropertiesValue();
				value.setLable(lables[i]);
				value.setValue(values[i]);
				value.setProperties(properties);
				list.add(value);
			}
			properties.setPropertiesValueList(list);
		}

		propertiesService.save(properties);
		redirectionUrl = "properties!list.action";
		return SUCCESS;
	}

	// 更新
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "properties.enumLable", message = "枚举名称不允许为空!")})
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		Properties persistent = propertiesService.load(id);
//		if(ids != null && ids.length > 0)
//		{
//			Iterator<PropertiesValue> ite = persistent.getPropertiesValueList().iterator();
//			
//			List<String> listIds = Arrays.asList(ids);
//			List<String> existIds = new ArrayList<String>();
//			
//			while(ite.hasNext())
//			{
//				PropertiesValue value = ite.next();
//				if(!listIds.contains(value.getId()))
//				{
//					ite.remove();
//				}
//				else
//				{
//					existIds.add(value.getId());
//				}
//			}
//			for (int i=0; i < lables.length; i++) {
//				PropertiesValue value = new PropertiesValue();
//				value.setId(ids[i]);
//				value.setLable(lables[i]);
//				value.setValue(values[i]);
//				value.setProperties(persistent);
//				if(!existIds.contains(ids[i]))
//				{
//					persistent.getPropertiesValueList().add(value);
//				}
//			}
//		}
		propertiesService.update(persistent);
		redirectionUrl = "properties!list.action";
		return SUCCESS;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public String[] getIds() {
		return ids;
	}

	public String[] getLables() {
		return lables;
	}

	public String[] getValues() {
		return values;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public void setLables(String[] lables) {
		this.lables = lables;
	}

	public void setValues(String[] values) {
		this.values = values;
	}

}